using System.Data;
using System.Data.SQLite;

namespace alpha
{
    public partial class Form1 : Form
    {
        public DataTable dt;
        public int curId;
        public Form1()
        {
            InitializeComponent();
            CreateSQLite();

            string currentDirectory = Directory.GetCurrentDirectory();
            string path = currentDirectory + @"\wordsdb.sqlite";
            using (SQLiteConnection cn = new SQLiteConnection("data source=" + path))
            {
                string sqldata = "SELECT * FROM words";
                using (SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(sqldata, cn))
                {
                    using (DataSet ds = new DataSet())
                    {
                        dataAdapter.Fill(ds);
                        dt = ds.Tables[0];
                    }
                }
            }
            Random random = new Random();
            curId = random.Next(0, dt.Rows.Count + 1);
            string wordMeaning = dt.Rows[curId][2].ToString();
            MeansLabel.Text = wordMeaning;
        }

        private void CreateSQLite()
        {
            string currentDirectory = Directory.GetCurrentDirectory();
            string path = currentDirectory + @"\wordsdb.sqlite";

            if (File.Exists(path))
            {
                //File.Delete(path);
                Console.WriteLine("sqlite exits");
                return;
            }

            SQLiteConnection cn = new SQLiteConnection("data source=" + path);
            cn.Open();

            SQLiteCommand cmd = new SQLiteCommand();
            cmd.Connection = cn;
            //cmd.CommandText = "CREATE TABLE words(id varchar(4),score int)";
            cmd.CommandText = "CREATE TABLE IF NOT EXISTS words(Id int,Word varchar(4),Meaning varchar(10))";
            cmd.ExecuteNonQuery();

            cmd.CommandText = "insert into words (Id,Word,Meaning) values (@Id,@Word,@Meaning)";
            var reader = new StreamReader(File.OpenRead(currentDirectory + @"\wordList.csv"));
            int id = 1;
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(',');
                string word = values[0].Replace("\"", "");
                string meaning = values[1].Replace("\"", "");

                cmd.Parameters.Clear();
                cmd.Parameters.Add(new SQLiteParameter("Id", id++));
                cmd.Parameters.Add(new SQLiteParameter("Word", word));
                cmd.Parameters.Add(new SQLiteParameter("Meaning", meaning));
                cmd.ExecuteNonQuery();
                Console.WriteLine("数据保存成功");
            }
            cn.Close();
        }



        private void wordText_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string word = dt.Rows[curId][0].ToString();
                if (wordText.Text == word)
                {
                    AnswerCheck.Text = "正确";
                }
                else
                {
                    AnswerCheck.Text = "错误";
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Random random = new Random();
            curId = random.Next(0, dt.Rows.Count + 1);
            string wordMeaning = dt.Rows[curId][2].ToString();
            MeansLabel.Text = wordMeaning;
            AnswerCheck.Text = "";
            wordText.Text = "";
        }

        private void wordText_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
